package model;

import java.io.File;
import java.util.ArrayList;

/**.
 * MoviesList
 * @author jaubertbenjamin
 *
 */
public class MoviesList {

	/**.
	 * Array list of movies
	 */
	private ArrayList<Movie> movies;

	/**.
	 * Constructor
	 */
	public MoviesList() {
		movies = new ArrayList<Movie>();
	}

	/**.
	 * Set MoviesList
	 * @param movies
	 */
	public MoviesList(ArrayList<Movie> movies){
		this.movies = movies;
	}

	/**.
	 * get Movies list
	 * @return arraList movieList
	 */
	public final ArrayList<Movie> getMovies() {
		return movies;
	}

	/**.
	 * add movies
	 * @param movie
	 */
	public final void addMovies(final Movie movie) {
		this.movies.add(movie);
	}

	/**.
	 * Order and store the movies into the given path
	 * @param path
	 */
	public final void order(String path){
		//create the base Folder
		path = path + "/moviesLibrary";
		File baseFolder = new File(path);
		baseFolder.mkdirs();

		//create the subfolders
		for (int i = 0; i < movies.size(); i++) { 
			if (movies.get(i).isCompleted()) {
				File subfolderByGenre = new File(path+"/"+movies.get(i).getGenre().get(0)); //first by Genre
				subfolderByGenre.mkdirs();
			}
		}

		new File(path + "/movies-not-completed").mkdirs();

		File[] list = baseFolder.listFiles();
		for (int i = 0; i < list.length; i++) { 
			if (list[i].isDirectory()) {
				for (int j = 0; j < movies.size(); j++) {
					if (movies.get(j).isCompleted()) {
						if (baseFolder.list()[i].equals(movies.get(j).getGenre().get(0))) {
							File subfolderByYear = new File(list[i]+"/"+movies.get(j).getDirector().get(0)+"/"+movies.get(j).getYear()); //second by director and by year
							subfolderByYear.mkdirs();
							
							File movie = new File(movies.get(j).getPath());
							String[] split = new File(movies.get(j).getPath()).getName().split("\\.");
						    String ext = split[split.length - 1];
						    String newPath = list[i]+"/"+movies.get(j).getDirector().get(0)+"/"+movies.get(j).getYear()+"/"+movies.get(j).getTitle().replaceAll("\\s+","_")+"."+ext;
							movie.renameTo(new File(newPath));
							movies.get(j).setPath(newPath);
						}
					}else if (!movies.get(j).isCompleted()) {
						File movie = new File(movies.get(j).getPath());
						String[] split = new File(movies.get(j).getPath()).getName().split("\\.");
					    String ext = split[split.length - 1];
					    String title=new File(movies.get(j).getPath()).getName().replaceFirst("[.][^.]+$", "");
					    String newPath = path+"/movies-not-completed"+"/"+title+"."+ext;
						movie.renameTo(new File(newPath));
						movies.get(j).setPath(newPath);
					}
				}
			}
		}
	}
	
	/**.
	 * Transform the ArrayList of Movie into a Array of String that stored all the movies titles
	 * @return array
	 */
	public final String[] transformIntoArray() {
		ArrayList<String> array = new ArrayList<String>();

		for( int i=0; i < movies.size(); i++) {
			if ((movies.get(i).getTitle() != null) && (movies.get(i).isCompleted())) {
				array.add(movies.get(i).getTitle());
			}
		}
		return array.toArray(new String[array.size()]);

	}

	/***.
	 * Find a movie by its title
	 * @param title
	 * @return the movie or null if not exist
	 */
	public Movie findMovie(String title) {
		for (int i = 0; i < movies.size(); i++) {
			if (movies.get(i).getTitle() != null) {
				if (movies.get(i).getTitle().equals(title)) {
					return movies.get(i);
				}
			}
		}
		return null;
	}

	/***.
	 * Find a movie by its path
	 * @param path
	 * @return
	 */
	public final Movie findMovieByPath(String path) {
		for (int i = 0; i < movies.size(); i++) {
			if (movies.get(i).getPath().equals(path)) {
				return movies.get(i);
			}
		}
		return null;
	}

	/**.
	 * Find movie
	 * @param search
	 * @return the array of found movies
	 */
	public final String[] searchMovies(String search) {
		ArrayList<String> array = new ArrayList<String>();

		for (int i = 0; i < movies.size(); i++) {
			if ((movies.get(i).isCompleted()) && (movies.get(i).getTitle().contains(search))) {
				array.add(movies.get(i).getTitle());
			}
		}
		return array.toArray(new String[array.size()]);
	}

	/**.
	 * Find imcomplete movies
	 * @return
	 */
	public final String[] findMoviesNotCompleted() {
		ArrayList<String> array = new ArrayList<String>();

		for (int i = 0; i < movies.size(); i++) {
			if (!movies.get(i).isCompleted()) {
				array.add(movies.get(i).getPath());
			}
		}
		return array.toArray(new String[array.size()]);
	}

	/***.
	 * Get the first available movie
	 * @return the movie
	 */
	public final Movie returnFirstAvailableMovie() {
		for (int i = 0; i < movies.size(); i++) {
			if (movies.get(i).isCompleted()) {
				return movies.get(i);
			}
		}
		return null;
	}

}
